<template>
  <div class="categories-container">
    <h2>药品分类管理</h2>
    
    <!-- 分类列表 -->
    <el-table :data="categories" style="width: 100%">
      <el-table-column prop="name" label="分类名称" />
      <el-table-column prop="description" label="描述" />
      <el-table-column label="操作">
        <template #default="{ row }">
          <el-button type="primary" size="small" @click="handleEdit(row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加/编辑分类对话框 -->
    <el-dialog
      :title="dialogTitle"
      v-model="dialogVisible"
      width="500px"
    >
      <el-form :model="categoryForm" label-width="80px">
        <el-form-item label="分类名称">
          <el-input v-model="categoryForm.name" />
        </el-form-item>
        <el-form-item label="描述">
          <el-input type="textarea" v-model="categoryForm.description" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSave">确定</el-button>
        </span>
      </template>
    </el-dialog>

    <!-- 添加分类按钮 -->
    <div class="add-button">
      <el-button type="primary" @click="handleAdd">添加分类</el-button>
    </div>
  </div>
</template>

<script>
import { ref, onMounted } from 'vue'
import { getCategories, createCategory, updateCategory, deleteCategory } from '../../api'
import { ElMessage, ElMessageBox } from 'element-plus'

export default {
  name: 'Categories',
  setup() {
    const categories = ref([])
    const dialogVisible = ref(false)
    const dialogTitle = ref('')
    const categoryForm = ref({
      name: '',
      description: ''
    })
    const isEdit = ref(false)

    const loadCategories = async () => {
      try {
        const { data } = await getCategories()
        categories.value = data
      } catch (error) {
        ElMessage.error('获取分类列表失败')
      }
    }

    const handleAdd = () => {
      dialogTitle.value = '添加分类'
      categoryForm.value = {
        name: '',
        description: ''
      }
      isEdit.value = false
      dialogVisible.value = true
    }

    const handleEdit = (row) => {
      dialogTitle.value = '编辑分类'
      categoryForm.value = { ...row }
      isEdit.value = true
      dialogVisible.value = true
    }

    const handleDelete = async (row) => {
      try {
        await ElMessageBox.confirm('确定要删除这个分类吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        await deleteCategory(row.id)
        ElMessage.success('删除成功')
        loadCategories()
      } catch (error) {
        if (error !== 'cancel') {
          ElMessage.error('删除失败')
        }
      }
    }

    const handleSave = async () => {
      try {
        if (isEdit.value) {
          await updateCategory(categoryForm.value.id, categoryForm.value)
        } else {
          await createCategory(categoryForm.value)
        }
        dialogVisible.value = false
        ElMessage.success(isEdit.value ? '更新成功' : '添加成功')
        loadCategories()
      } catch (error) {
        ElMessage.error(isEdit.value ? '更新失败' : '添加失败')
      }
    }

    onMounted(() => {
      loadCategories()
    })

    return {
      categories,
      dialogVisible,
      dialogTitle,
      categoryForm,
      handleAdd,
      handleEdit,
      handleDelete,
      handleSave
    }
  }
}
</script>

<style scoped>
.categories-container {
  padding: 20px;
}

.add-button {
  margin-top: 20px;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}
</style> 